package presenter.code.entity;

import utils.TextUtils;

import java.util.List;

public class AccountEntity implements Comparable<AccountEntity> {
    private boolean overtime;
    private String account;
    private String login;
    private String path;
    private List<CodeEntity> codes;

    public void set(String account, String login, boolean overtime) {
        if (TextUtils.isEmpty(this.account) && TextUtils.isEmpty(this.login)) {//只保存一次,防止多次登录的情况
            this.account = account;
            this.login = login;
            this.overtime = overtime;
        }
    }

    public boolean validAccount() {
        return !TextUtils.isEmpty(account) && !TextUtils.isEmpty(login);
    }

    //登录超时
    public boolean overTime() {
        return overtime;
    }

    public String accountTip() {
        return String.format("jq%s\n%s", account, loginTime());
    }

    public List<CodeEntity> codes() {
        return codes;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void set(List<CodeEntity> codes) {
        this.codes = codes;
    }

    public String show() {
        return String.format("%s\n%s\n", tip(), items());
    }

    private String tip() {
        return String.format("账号: jq%s,  登录时间: %s,  文件位置: %s", account, loginTime(), path);
    }

    private String loginTime() {
        return overtime ? String.format("【%s】", login) : String.format("[%s]", login);
    }

    private String items() {
        if (codes == null || codes.isEmpty()) {
            return "暂未发现Code!";
        } else {
            StringBuilder code = new StringBuilder();
            StringBuilder timeLong = new StringBuilder();
            StringBuilder time = new StringBuilder();
            for (CodeEntity item : codes) {
                code.append(item.code());
                code.append("\t");
                timeLong.append(item.time());
                timeLong.append("\t");

                time.append(item.stamp());
                time.append("\t");
            }
            return String.format("%s\n%s\n%s", code, timeLong, time);
        }
    }

    @Override
    public int compareTo(AccountEntity item) {
        return fixedInt(account) - fixedInt(item.account);
    }

    private int fixedInt(String content) {
        try {
            return Integer.parseInt(content);
        } catch (Exception ex) {
            return 0;
        }
    }
}
